/*
 * @Description: 基础流程模型xml
 * @Author: DHL
 * @Date: 2022-04-20 13:39:06
 * @LastEditors: DHL
 * @LastEditTime: 2023-07-28 13:47:10
 */

import { buildLengthUUID } from '@/utils/uuid'

const startEventId = buildLengthUUID(7).toLocaleLowerCase()
const startEventOutgoingId = buildLengthUUID(7).toLocaleLowerCase()
const documentMakingId = buildLengthUUID(7).toLocaleLowerCase()
const documentMakingOutgoingId = buildLengthUUID(7).toLocaleLowerCase()
const userTaskId = buildLengthUUID(7).toLocaleLowerCase()
const userTaskOutgoingId = buildLengthUUID(7).toLocaleLowerCase()
const endEventId = buildLengthUUID(7).toLocaleLowerCase()

export default (
  key: string,
  name: string,
  category: string,
  description: string,
  type = 'flowable'
) => {
  if (!type) type = 'flowable'

  const TYPE_TARGET: any = {
    activiti: 'http://activiti.org/bpmn',
    camunda: 'http://bpmn.io/schema/bpmn',
    flowable: 'http://flowable.org/bpmn'
  }

  return `
  <?xml version="1.0" encoding="UTF-8"?>
  <bpmn2:definitions 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL"
    xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
    xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
    xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
    id="diagram_${key}"
    targetNamespace="${category}">

    <bpmn2:process id="${key}" name="${name}" isExecutable="true">
      <bpmn2:documentation>${description}</bpmn2:documentation>

      <bpmn2:startEvent id="Event_${startEventId}" name="开始">
        <bpmn2:outgoing>Flow_${startEventOutgoingId}</bpmn2:outgoing>
      </bpmn2:startEvent>

      <bpmn2:userTask id="Activity_${documentMakingId}" name="制单" flowable:exclusive="false" flowable:category="TODO" flowable:assignee="\${initiator}" flowable:documentMaking="true">
        <bpmn2:extensionElements>
          <flowable:approvalRules>{"category":"manualApproval","assignedRules":{"type":"initiator","optionalWay":null,"assigneeList":[],"postId":null,"levelChecked":false,"level":null},"multiInstanceRules":{"approvalWay":""},"timeLimitRules":{}}</flowable:approvalRules>
        </bpmn2:extensionElements>
        <bpmn2:incoming>Flow_${startEventOutgoingId}</bpmn2:incoming>
        <bpmn2:outgoing>Flow_${documentMakingOutgoingId}</bpmn2:outgoing>
      </bpmn2:userTask>

      <bpmn2:sequenceFlow id="Flow_${startEventOutgoingId}" sourceRef="Event_${startEventId}" targetRef="Activity_${documentMakingId}" />

      <bpmn2:userTask id="Activity_${userTaskId}" name="发起人自选" flowable:exclusive="false" flowable:category="TODO" flowable:assignee="\${assignee}">
        <bpmn2:extensionElements>
          <flowable:approvalRules>{"category":"manualApproval","assignedRules":{"type":"optional","optionalWay":"more","assigneeList":[],"postId":null,"levelChecked":false,"level":null},"multiInstanceRules":{"approvalWay":"sequential"},"timeLimitRules":{}}</flowable:approvalRules>
        </bpmn2:extensionElements>
        <bpmn2:incoming>Flow_${documentMakingOutgoingId}</bpmn2:incoming>
        <bpmn2:outgoing>Flow_${userTaskOutgoingId}</bpmn2:outgoing>
        <bpmn2:multiInstanceLoopCharacteristics isSequential="true" flowable:collection="assigneeList_Activity_${userTaskId}" flowable:elementVariable="assignee">
          <bpmn2:completionCondition xsi:type="bpmn2:tFormalExpression">\${nrOfCompletedInstances == nrOfInstances}</bpmn2:completionCondition>
        </bpmn2:multiInstanceLoopCharacteristics>
      </bpmn2:userTask>

      <bpmn2:sequenceFlow id="Flow_${documentMakingOutgoingId}" sourceRef="Activity_${documentMakingId}" targetRef="Activity_${userTaskId}" />

      <bpmn2:endEvent id="Event_${endEventId}" name="结束">
        <bpmn2:incoming>Flow_${userTaskOutgoingId}</bpmn2:incoming>
      </bpmn2:endEvent>

      <bpmn2:sequenceFlow id="Flow_${userTaskOutgoingId}" sourceRef="Activity_${userTaskId}" targetRef="Event_${endEventId}" />
      
    </bpmn2:process>
    
    <bpmndi:BPMNDiagram id="BPMNDiagram_1">
      <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="${key}">

        <bpmndi:BPMNShape id="Event_${startEventId}_di" bpmnElement="Event_${startEventId}">
          <dc:Bounds x="222" y="172" width="36" height="36" />
          <bpmndi:BPMNLabel>
            <dc:Bounds x="229" y="215" width="22" height="14" />
          </bpmndi:BPMNLabel>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape id="Activity_${documentMakingId}_di" bpmnElement="Activity_${documentMakingId}">
          <dc:Bounds x="310" y="150" width="100" height="80" />
          <bpmndi:BPMNLabel />
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape id="Activity_${userTaskId}_di" bpmnElement="Activity_${userTaskId}">
          <dc:Bounds x="470" y="150" width="100" height="80" />
          <bpmndi:BPMNLabel />
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape id="Event_${endEventId}_di" bpmnElement="Event_${endEventId}">
          <dc:Bounds x="632" y="172" width="36" height="36" />
          <bpmndi:BPMNLabel>
            <dc:Bounds x="639" y="215" width="22" height="14" />
          </bpmndi:BPMNLabel>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNEdge id="Flow_${startEventOutgoingId}_di" bpmnElement="Flow_${startEventOutgoingId}">
          <di:waypoint x="258" y="190" />
          <di:waypoint x="310" y="190" />
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge id="Flow_${documentMakingOutgoingId}_di" bpmnElement="Flow_${documentMakingOutgoingId}">
          <di:waypoint x="410" y="190" />
          <di:waypoint x="470" y="190" />
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge id="Flow_${userTaskOutgoingId}_di" bpmnElement="Flow_${userTaskOutgoingId}">
          <di:waypoint x="570" y="190" />
          <di:waypoint x="632" y="190" />
        </bpmndi:BPMNEdge>
        
      </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>

  </bpmn2:definitions>
  `
}
